package cn.zjj.org.controller;

import cn.zjj.basic.anno.PreAuthorize;
import cn.zjj.basic.exception.BusinessException;
import cn.zjj.basic.util.ExcelUtils;
import cn.zjj.basic.util.JsonResult;
import cn.zjj.basic.util.PageList;
import cn.zjj.org.domain.Shop;
import cn.zjj.org.domain.ShopAuditLog;
import cn.zjj.org.dto.ShopDto;
import cn.zjj.org.query.ShopQuery;
import cn.zjj.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class ShopController {

    @Resource
    private IShopService shopService;

    //返回echarts报表数据
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return shopService.getEcharts();
    }





    //导出
    @GetMapping("/exportExcel")
    @ApiOperation(value = "导出" )
    public void exportExcel(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.selectAll();
            //List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,
            // HttpServletResponse response
            ExcelUtils.exportExcel(shops,null,"店铺数据",Shop.class,"shop.xlsx",response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**导入
     * title:name age sex 那一栏,给一行 excel中好看些
     * @param file
     */
    @PostMapping("/importExcel")
    @ApiOperation(value = "导入" )
    public void importExcel(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            //MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
            List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);

            shopService.patchInsert(shops);
           /* shops.forEach(System.out::println);*/


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }





    //人工审核通过
    @PostMapping("/pass")
    @ApiOperation(value = "审核通过" )
    public JsonResult auditPass(@RequestBody ShopAuditLog shopAuditLog){

        try {
           /* shopService.auditPass(shopAuditLog);*/
            shopService.audit(shopAuditLog,true);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }

    }

    //人工审核驳回
    @PostMapping("/reject")
    @ApiOperation(value = "审核驳回" )
    public JsonResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            /*shopService.auditReject(shopAuditLog);*/
            shopService.audit(shopAuditLog,false);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }

    }



    //商家入驻
    @PutMapping("/settlement")
    @ApiOperation(value = "店铺入驻" )
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);

            return JsonResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return new JsonResult().runtimeException(e.getMessage());
        }catch (Exception e) {
           e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }


    }


    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    @PreAuthorize(name = "商铺列表",value = "shop:list")
    public List<Shop> selectAll(){

        return shopService.selectAll();
    }

    //查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据" )
    public Shop selectById(@PathVariable("id") Long id){
        return shopService.selectById(id);
    }

    //删除单个
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个数据" )
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //添加或修改
    @PutMapping
    @ApiOperation(value = "添加或修改数据" )
    public JsonResult updateOrAdd(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                shopService.insert(shop);
            }else {
                shopService.update(shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //分页和高级查询
    @PostMapping
    @ApiOperation(value = "分页查询或高级查询" )
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.selectQuery(query);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult delete(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }
}
